﻿
using BaseUtils.Helper;
using BaseUtils.Logger;
using DriveManage.SocketClient;
using Service.src.NW.Model;
using System;
using System.Collections.Generic;
using System.Net.Sockets;

namespace Service.src.Flow.Server
{
    public class EventServer
    {

        public SystemEvent AddEvent(int DBIndex, SystemEvent e)
        {
            if (e == null)
            {
                throw new Exception("更新事件时，对象不允许为空");
            }
            if (e?.Id > 0)
            {
                LoggerService.Warning($"警告：新增事件数据时Id异常，数据：{e.ToJson()}。");
                e = UpdateEvent(DBIndex, e);
            }
            else
            {
                e = AppData.DatabaseCache[DBIndex].Install(e);
                LoggerService.Info($"数据新增成功：{e.ToJson()}");
            }
            return e;
        }

        /// <summary>
        /// 更新对象
        /// </summary>
        /// <param name="eventJson"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public SystemEvent UpdateEvent(int DBIndex, SystemEvent e)
        {
            if (e == null)
            {
                throw new Exception("更新事件时，对象不允许为空");
            }
            if (e.Id <= 0)
            {
                throw new Exception($"更新事件时，对象数据ID异常:{e.Id},对象：{e.ToJson()}");
            }
            int i = 0;
            if (e?.Id > 0)
            {
                i = AppData.DatabaseCache[DBIndex].Update(e);
            }
            if (i == 1)
            {
                LoggerService.Info($"数据更新成功：{e.ToJson()}");
            }
            return e;
        }

        internal bool DeleteEvent(int DBIndex, int Id)
        {

            SystemEvent e = new SystemEvent();
            e.Id = Id;
            bool res = AppData.DatabaseCache[DBIndex].Delete(e);
            if (res)
            {
                LoggerService.Info($"事件数据删除成功：ID{Id}");
            }
            else
            {
                LoggerService.Info($"事件数据删除失败：ID{Id}");
            }
            return res;
        }


        /// <summary>
        /// 查找全部
        /// </summary>
        /// <returns></returns>
        internal List<SystemEvent> SearchEvent(int DBIndex)
        {
            List<SystemEvent> events = AppData.DatabaseCache[DBIndex].QueryAll<SystemEvent>();
            return events;
        }

        internal List<TcpClient> GetConnectedClients(int ServerId)
        {
            if (!AppData.SocketHandlers.ContainsKey(ServerId))
            {
                return new List<TcpClient>();
            }
            ISocketHandler socket = AppData.SocketHandlers[ServerId];

            return socket.ConnectedClients;
        }

        internal bool SendMessage(int ServerId, int ClientIndex, string Message)
        {
            if (!AppData.SocketHandlers.ContainsKey(ServerId) && AppData.SocketHandlers[ServerId].ConnectedClients.Count > 0)
            {
                return false;
            }
            TcpClient c = AppData.SocketHandlers[ServerId].ConnectedClients[ClientIndex];
            AppData.SocketHandlers[ServerId].SendMessage(c, Message);
            return true;
        }
    }
}
